<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Main extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('wus');
		$this->load->model('sync_model');
	}

	public function test_call(){
		//$this->sync_model->call_test();
		echo random_string('alnum', 20);
	}

	public function syncCustomers(){
		echo json_encode($this->sync_model->getCustomers());
	}

	public function syncAttempt(){
		$data = array(':p_year_month' => '');
		$this->sync_model->gen_table($data);
		//$this->wus->call_acct_chg();
		$json = $_POST["attempLog"];

		//Remove Slashes
		if (get_magic_quotes_gpc()) {
			$json = stripslashes($json);
		}

		//Decode JSON into an Array
		$data = json_decode($json);
		$this->wus->debug(count($data));
		//Util arrays to create response JSON
		$a = array();
		$b = array();

		//insert debug statements
		//$r = storeDebug($db, "debug4", $json);

		//sync variables
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "BILL";
		//$device = $_POST["deviceID"];
		$id = 0;
		$count = 0;
		//$i = 0;
		 for ($i = 0; $i < count($data); $i++) {

			$data2 = array(
				':p_user_id' 	   => $data[$i]->USER_ID,
				':p_account_id'    => $data[$i]->ACCT_ID,
				':p_read_time' 	   => $data[$i]->TIME,
				':p_read_log_code' => $data[$i]->CODE,
			);

			$this->sync_model->sync_attempts($data2);
		  
		}
		//Post JSON response back to Android Application
		echo json_encode($a);
	}

	public function syncusedchg(){
		$data = array(':p_year_month' => '');
		$this->sync_model->gen_table($data);
		//$this->wus->call_acct_chg();
		$json = $_POST["usedJSON"];

		//Remove Slashes
		if (get_magic_quotes_gpc()) {
			$json = stripslashes($json);
		}

		//Decode JSON into an Array
		$data = json_decode($json);
		$this->wus->debug(count($data));
		//Util arrays to create response JSON
		$a = array();
		$b = array();

		//insert debug statements
		//$r = storeDebug($db, "debug4", $json);

		//sync variables
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "BILL";
		//$device = $_POST["deviceID"];
		$id = 0;
		$count = 0;
		//$i = 0;
		 for ($i = 0; $i < count($data); $i++) {
			//Store Bill into MySQL DB
			$data2 = array(
				':p_account_id' => $data[$i]->ACCT_ID,
				':p_chg_amt' 	=> $data[$i]->CHG_AMT,
				':p_chg_code' 	=> $data[$i]->CHG_CODE,
				':p_chg_desc'	=> $data[$i]->CHG_DESC,
				':p_yearMonth' 	=> $data[$i]->CHG_YM,
				':p_user_id' 	=> $data[$i]->USER_ID
			);

			$this->sync_model->sync_used($data2);

		}
		//Post JSON response back to Android Application
		echo json_encode($a);
	}

	

	public function synczones(){
		$zones = $this->sync_model->sync_getZones();
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($zones as $row) {
			$b["ZONE_CODE"] = $row->zone_name;
			$b["MEANING"]   = $row->description;
			array_push($a, $b);
		}
		echo json_encode($a);
	}

	public function synczones2(){

		$zones = $_POST["ZONE"];

		$zones2 = $this->sync_model->sync_getZones2();
		$a = array();
		$b = array();

		foreach($zones2 as $row){
			$b["PARENT_ZONE"]	= $row->parent_zone_name;
			$b["ZONE2_NAME"]	= $row->zone_name;
			array_push($a , $b);	
		}
		echo json_encode($a);
	}




	public function syncusers(){
		//$this->wus->call_acct_chg();
		$users = $this->sync_model->sync_getUser();
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($users as $row) {
			$b["USER_NAME"] = $row->username;
			$b["USER_PASSWORD"] = $row->password;
			$b["USER_ID"] = $row->user_id;
			array_push($a, $b);
		}
		echo json_encode($a);

	}

	public function syncmmeters(){
		//$this->wus->call_acct_chg();
		$mmeters = $this->sync_model->get_mother_meters();
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($mmeters as $mm) {
			$b["MM_ID"] 	= $mm->mmeter_id;
			$b["MM_NAME"] 	= $mm->mother_name;
			$b["MM_SERIAL"] = $mm->mother_serial;
			$b["MM_DESC"] 	= $mm->description;
			$b["MM_PREV"]	= $mm->current;
			$b["MM_MNTH"]	= $mm->last_read_month;
			$b["MM_YEAR"]	= $mm->last_read_year;
			$b["MM_DATE"]	= $mm->last_read_date;
			array_push($a, $b);
		}
		echo json_encode($a);

	}


	public function syncMm_IB(){
		//$this->wus->call_acct_chg();
		$mmeters = $this->sync_model->get_mother_meter_readings();
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($mmeters as $mm) {
			$b["MM_ID"] 	= $mm->mmeter_id;
			$b["MM_NAME"] 	= $mm->mother_name;
			$b["MM_PDATE"]	= $mm->prev_date;
			$b["MM_PREV"] 	= $mm->previous;
			$b["MM_CURR"] 	= $mm->current;
			$b["MM_CONS"]	= $mm->consumed;
			$b["MM_MNTH"] 	= $mm->last_read_month;
			$b["MM_YEAR"] 	= $mm->last_read_year;
			$b["MM_DATE"] 	= $mm->last_read_date;
			array_push($a, $b);
		}
		echo json_encode($a);

	}

	public function syncMm_OB(){
		
		//$this->wus->call_acct_chg();
		$json = $_POST["mmOutJSON"];

		//Remove Slashes
		$data = json_decode($json);
		//Util arrays to create response JSON
		$a = array();
		$b = array();

		//insert debug statements
		//$r = storeDebug($db, "debug4", $json);

		//sync variables
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "BILL";
		//$device = $_POST["deviceID"];
		$id = 0;
		$count = 0;
		//$i = 0;
		 for ($i = 0; $i < count($data); $i++) {
			//Store Bill into MySQL DB
			//$this->gen_pdf($data[$i]->CUST_ID,$data[$i]->BILL_YEAR,$data[$i]->BILL_MONTH,$data[$i]->TOTAL_DUE,$data[$i]->BILL_BAL,$data[$i]->DUE_DATE,$data[$i]->BILL_DATE,$data[$i]->READ_DATE,$data[$i]->PREV_DATE);
		  
			$data2 = array(
				  ':p_mmeter_id'    	=> $data[$i]->MM_ID
				 ,':p_read_month'     	=> $data[$i]->MM_MONTH 
				 ,':p_read_year'     	=> $data[$i]->MM_YEAR 
				 ,':p_previous_read' 	=> $data[$i]->MM_PREAD 
				 ,':p_prev_read_date'	=> $data[$i]->MM_PDATE 
				 ,':p_reading'  		=> $data[$i]->MM_CREAD 
				 ,':p_consumed'      	=> $data[$i]->MM_CONS 
				 ,':p_read_date'     	=> $data[$i]->MM_CDATE 
				 ,':p_user_id'    		=> $data[$i]->READ_BY 
			);
			
			$res = $this->sync_model->save_reads($data2);
			foreach ($res as $key) {
				$c = $key->error;
			}
		
			if ($c == 1) {
				$b["MM_ID"] 	= $data[$i]->MM_ID;
				$b["MM_MNTH"] 	= $data[$i]->MM_MONTH;
				$b["MM_YEAR"] 	= $data[$i]->MM_YEAR;
				$b["SYNC_FLAG"] = 'N';
				array_push($a, $b);
				//$count = $count + 1;
			} else {
				
				$b["MM_ID"] 	= $data[$i]->MM_ID;
				$b["MM_MNTH"] 	= $data[$i]->MM_MONTH;
				$b["MM_YEAR"]  	= $data[$i]->MM_YEAR;
				$b["SYNC_FLAG"] = 'Y';
				array_push($a, $b);
			}
		  
		}
		//Post JSON response back to Android Application
		echo json_encode($a);
	}

	public function syncchg(){
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		//$sd = "CUSTOMER";
		$zone = $_POST["ZONE"];
		$user = $_POST["USER"];
		$id = 0;

		//Get Customers
		$charges = $this->sync_model->sync_chg($zone,$user);
		$a = array();
		$b = array();

		//Check if records are returned
//var_dump($charges);
		foreach ($charges as $row) {
			$b["AI"] = $row->acct_id;
			$b["LI"] = $row->list_id;
			$b["YM"] = $row->yearmonth;
			$b["SN"] = $row->seq_num;
			$b["CC"] = $row->charge_code;
			$b["DC"] = $row->description;
			$b["CA"] = $row->chg_amount;
			
			array_push($a, $b);

		}
		echo json_encode($a);
	}

	public function syncusedchgIN(){
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		//$sd = "CUSTOMER";
		//$device = $_POST["deviceID"];
		$id = 0;

		//Get Customers
		$charges = $this->sync_model->sync_used_in();
		$a = array();
		$b = array();

		//Check if records are returned
//var_dump($charges);
		foreach ($charges as $row) {
			$b["AI"] = $row->account_id;
			$b["CA"] = $row->charge_amount;
			$b["CC"] = $row->charge_code;
			$b["CD"] = $row->charge_description;
			$b["YM"] = $row->yearMonth;
			
			array_push($a, $b);

		}
		echo json_encode($a);
	}

	public function syncdischg(){
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		//$sd = "CUSTOMER";
		//$device = $_POST["deviceID"];
		$id = 0;

		//Get Customers
		$charges = $this->sync_model->sync_dischg();
		$a = array();
		$b = array();

		//Check if records are returned
//var_dump($charges);
		foreach ($charges as $row) {
			$b["LI"] = $row->list_id;
			$b["CC"] = $row->charge_code;
			$b["DC"] = $row->description;
			$b["OV"] = $row->value;
			
			array_push($a, $b);

		}
		echo json_encode($a);
	}

	public function syncaccount() {
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "CUSTOMER";
		$zoneL = $_POST["ZONE"];
		$userL = $_POST["USER"];
		$id = 0;

		//Get Customers
		$custs = $this->sync_model->sync_cust($zoneL,$userL);
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($custs as $row) {
			$b["CI"]  = $row->account_id;
			$b["AN"]  = $row->account_number;
			$b["SN"]  = $row->serial_number;
			$b["LAT"] = $row->latitude;
			$b["LNG"] = $row->longitude;
			$b["NA"]  = $row->name;
			$b["AC"]  = $row->list_id;
			$b["AT"]  = $row->attention;
			$b["SA"]  = $row->street_address;
			$b["SA2"] = $row->street_address2;
			$b["CT"]  = $row->city;
			$b["PR"]  = $row->meter_reading;
			$b["PD"]  = $row->reading_date;
			$b["BB"]  = $row->bill_balance;
			$b["AP"]  = $row->adv_pay;
			$b["DD"]  = $row->latest_due_date;
			$b["LP"]  = $row->latest_pay_date;
			$b["CY"]  = $row->customer_type;
			$b["JC"]  = $row->wo_code;
			$b["JD"]  = $row->wo_date;
			$b["JLR"] = $row->wo_last_reading;
			$b["JNR"] = $row->wo_new_reading;
			$b["EN"]  = $row->status;
			$b["3M"]  = $row->average;
			$b["ZN"]  = $row->zone;
			$b["BK"]  = $row->book;
			$b["MS"]  = $row->meter_seq;
			$b["RI"]  = $row->reader_id;
			$b["MZ"]  = $row->meter_size;
			array_push($a, $b);

		}
		echo json_encode($a);
	}

	public function syncrates() {

		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "WRATE";
		// $device = $_POST["deviceID"];
		$id = 0;

		//Get Rates
		$rates = $this->sync_model->syncRates();
		$a = array();
		$b = array();

		//Check if records are returned

		foreach ($rates as $row) {
			$b["NAME"] 	 	= $row->rate_name;
			$b["LIST_ID"]	= $row->list_id;
			$b["ACCT_TYPE"] = $row->acct_type;
			$b["METER_SIZE"]= $row->meter_size;
			$b["CHG_CODE"]	= $row->charge_code;
			$b["DESC"] 		= $row->description;
			$b["USG"] 		= $row->usage_qty;
			$b["RATE"] 		= $row->rate;
			$b["MIN_FLAG"] 	= $row->min_flag;
			array_push($a, $b);
		}
		echo json_encode($a);

	}

	public function sync_meter_reading() {

		//sync variables
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "READING";
		$zoneL = $_POST["ZONE"];
		$userL = $_POST["USER"];
		$id = 0;
		$count = 0;

		//Get Customers
		$reads = $this->sync_model->sync_water_readings($zoneL,$userL);
		$a = array();
		$b = array();

		//Check if records are returned
		foreach ($reads as $row) {
			$b["BM"]  = $row->bill_month;
			$b["BY"]  = $row->bill_year;
			$b["PD"]  = $row->prev_date;
			$b["RD"]  = $row->read_date;
			$b["CN"]  = $row->consumed;
			$b["BD"]  = $row->bill_date;
			$b["DD"]  = $row->due_date;
			$b["TD"]  = $row->total_due;
			$b["PR"]  = $row->prev_read;
			$b["CR"]  = $row->cur_read;
			$b["RA"]  = $row->read_amt;
			$b["CC"]  = $row->currchg;
			$b["BB"]  = $row->bill_bal;
			$b["PRT"] = $row->print_date;
			$b["JC"]  = $row->wo_code;
			$b["JD"]  = $row->wo_date;
			$b["JLR"] = $row->wo_last_reading;
			$b["JNR"] = $row->wo_new_reading;
			$b["CI"]  = $row->customer_id;
			$b["SF"]  = $row->sp_flag;
			$b["SR"]  = $row->sp_readType;
			$b["SN"]  = $row->sp_reason;
			$b["AP"]  = $row->adv_pay;
			array_push($a, $b);
			//$count = $count + 1;
		}
		
		echo json_encode($a);


	}
	public function test(){
		$data2 = array(
				'account_id' 	=> 618,
				'prev_read' 	=> '$data[$i]->PREV_READ',
				'prev_date' 	=> '$data[$i]->PREV_DATE',
				'meter_reading' => '$data[$i]->CUR_READ',
				'reading_date' 	=> '$data[$i]->READ_DATE',
				'consumed' 		=> '$data[$i]->CONSUMED',
				'bill_date' 	=> '$data[$i]->BILL_DATE',
				'bill_month' 	=> '07',
				'bill_year' 	=> '2016',
				'read_amt' 		=> '$data[$i]->READ_AMT',
				'bill_bal' 		=> '$data[$i]->BILL_BAL',
				'surchg' 		=> '$data[$i]->SURCHG',
				'lpaychg' 		=> '$data[$i]->LPAYCHG',
				'curchg' 		=> '$data[$i]->CURRCHG',
				'total_due' 	=> '$data[$i]->TOTAL_DUE',
				'due_date' 		=> '$data[$i]->DUE_DATE',
				'print_flag' 	=> '$data[$i]->PRINT_FLAG',
				'print_by' 		=> '$data[$i]->PRINT_BY',
				'print_date' 	=> '$data[$i]->PRINT_DATE',
				'sp_flag' 		=> '$data[$i]->SP_FLAG',
				'sp_readType' 	=> '$data[$i]->SP_READ',
				'sp_reason'		=> '$data[$i]->SP_RSN',
				'rates'			=> 'test'
			);
			$res = $this->sync_model->sync_bills($data2);
			//echo $res;
			var_dump($res);
			
			echo json_encode($a);
			//var_dump($res);
	}

	public function sync_bills() {
		$data = array(':p_year_month' => '');
		$this->sync_model->gen_table($data);
		//$this->wus->call_acct_chg();
		$json = $_POST["billJSON"];

		//Remove Slashes
		if (get_magic_quotes_gpc()) {
			$json = stripslashes($json);
		}

		//Decode JSON into an Array
		// mother meter = 1593
		$data = json_decode($json);
		$this->wus->debug(count($data));
		//Util arrays to create response JSON
		$a = array();
		$b = array();

		//insert debug statements
		//$r = storeDebug($db, "debug4", $json);

		//sync variables
		date_default_timezone_set('asia/singapore');
		$st = date('d-M-y H:i:s');
		$sd = "BILL";
		//$device = $_POST["deviceID"];
		$id = 0;
		$count = 0;
		//$i = 0;
		 for ($i = 0; $i < count($data); $i++) {
			//Store Bill into MySQL DB
			//$this->gen_pdf($data[$i]->CUST_ID,$data[$i]->BILL_YEAR,$data[$i]->BILL_MONTH,$data[$i]->TOTAL_DUE,$data[$i]->BILL_BAL,$data[$i]->DUE_DATE,$data[$i]->BILL_DATE,$data[$i]->READ_DATE,$data[$i]->PREV_DATE);
		  
			$data2 = array(
				'account_id' 	=> $data[$i]->CUST_ID,
				'prev_read' 	=> $data[$i]->PREV_READ,
				'prev_date' 	=> $data[$i]->PREV_DATE,
				'meter_reading' => $data[$i]->CUR_READ,
				'reading_date' 	=> $data[$i]->READ_DATE,
				'consumed' 		=> $data[$i]->CONSUMED,
				'bill_date' 	=> $data[$i]->BILL_DATE,
				'bill_month' 	=> $data[$i]->BILL_MONTH,
				'bill_year' 	=> $data[$i]->BILL_YEAR,
				'read_amt' 		=> $data[$i]->READ_AMT,
				'bill_bal' 		=> $data[$i]->BILL_BAL,
				'curchg' 		=> $data[$i]->CURRCHG,
				'total_due' 	=> $data[$i]->TOTAL_DUE,
				'due_date' 		=> $data[$i]->DUE_DATE,
				'print_flag' 	=> $data[$i]->PRINT_FLAG,
				'print_by' 		=> $data[$i]->PRINT_BY,
				'print_date' 	=> $data[$i]->PRINT_DATE,
				'sp_flag' 		=> $data[$i]->SP_FLAG,
				'sp_readType' 	=> $data[$i]->SP_READ,
				'sp_reason'		=> $data[$i]->SP_RSN,
				'rates'			=> $data[$i]->MAX_RATE,
				'adv_pay'		=> $data[$i]->ADV_PAY,
				'remarks'		=> $data[$i]->REMARKS,
				'findings'		=> $data[$i]->FINDINGS
			);
		
				$res = $this->sync_model->sync_bills($data2);
				
      #for logging purpose only
        
		
			foreach ($res as $key) {
				$c = $key->status;
			}
			
			if ($c == TRUE) {
				$b["cust_id"] = $data[$i]->CUST_ID;
				$b["bill_month"] = $data[$i]->BILL_MONTH;
				$b["bill_year"] = $data[$i]->BILL_YEAR;
				$b["status"] = 'Y';
				array_push($a, $b);
				$count = $count + 1;
			} else {
				$b["cust_id"] = $data[$i]->CUST_ID;
				$b["bill_month"] = $data[$i]->BILL_MONTH;
				$b["bill_year"] = $data[$i]->BILL_YEAR;
				$b["status"] = 'N';
				array_push($a, $b);
			}
		  
		}
		//Post JSON response back to Android Application
		echo json_encode($a);
	}

	public function insert_invoice() {
		$data = array(
			'p_invoice_num' 	=> 1,
			'p_account_number' 	=> '39',
			'p_account_type' 	=> '23',
			'p_invoice_type' 	=> '23',
			'p_address_id'	 	=> '23',
			'p_invoice_date' 	=> '23',
			'p_charge_list_id' 	=> '2',
			'p_due_date' 		=> '23',
			'p_rec_acct_id' 	=> '23',
			'p_description' 	=> '23',
			'p_status' 			=> '23',
			'p_prev_bill_bal' 	=> '12312',
			'p_invoice_total' 	=> '12314',
			'p_invoice_balance' => '12314',
		);
		$this->sync_model->insert_invoice($data);
	}

	public function checker(){
		$id = $_GET['id'];
		$q = $this->sync_model->check($id);
	}

	public function call_ac(){
		$this->wus->call_acct_chg();
	}

	function sample() {
		var_dump($this->session->all_userdata());
	}

	public function reroute($mod_id, $men_id, $form_id) {

		//$this->session->sess_destroy();
		$module_name = $this->wus->getModName($mod_id);
		$mod_name 	 = $this->wus->get_mod_name($mod_id);
		$menu_name 	 = $this->wus->get_menu_name($men_id);
		$form_roles  = $this->wus->get_form_roles($form_id);

		//$module = explode('application/views/', $mod_name[0]->dir_path);
		$module = $mod_name[0]->dir_path;
		if ($mod_id == '' && $men_id == '' && $form_id == '') {
			$mod_id = 0;
			$men_id = 0;
			$form_id = 0;
		}

		foreach ($form_roles as $key) {
			$this->session->set_userdata(array(
				'create' => $key->allow_create,
				'update' => $key->allow_update,
				'delete' => $key->allow_delete,
				'readonly' => $key->read_only,
			));
		}

		$form_name = $this->wus->get_form_name($form_id);
		$exe = explode('.php', $form_name[0]->exe_file_name);
		$forroute = $module . "/" . $exe[0];
		$formodel = $module . "/" . $exe[0] . "_model";
		$formodel_file = $exe[0] . "_model";

		$forload = $exe[0];

		$formain = $module . "/" . $form_name[0]->program_type . "/" . $exe[0] . "_main";
		$foradd = $module . "/" . $form_name[0]->program_type . "/" . $exe[0] . "_add";
		$foredit = $module . "/" . $form_name[0]->program_type . "/" . $exe[0] . "_edit";
		$forview = $module . "/" . $form_name[0]->program_type . "/" . $exe[0] . "_view";

	
		//setcookie($exe[0], $cookie_value, time() + (86400 * 30), "/");




		$this->session->set_userdata(array(
			'title' => $mod_name[0]->module_name . ' > ' . $menu_name[0]->menu_name . ' : ' . $form_name[0]->form_name,
			'formodel' => $formodel,
			'formodelfile' => $formodel_file,
			'forajax' => $forroute,
			'formain' => $formain,
			'foradd' => $foradd,
			'foredit' => $foredit,
			'forview' => $forview,
			'forload' => $forload,
			'moduleid' => $mod_id,
			'menuid' => $men_id,
			'formid' => $form_id,
		));

		//$this->session->set_userdata($data);
		//echo "<pre>". print_r($this->session->userdata('forajax'),1). "</pre>";
		//var_dump($data['title']);
		//var_dump($forroute);
		redirect($forroute);
	}

	public function haha(){
		$this->wus->tae();
		//$this->gen_pdf("1","2016","02","514.90","326.50","25-Feb-16","10-Feb-16","10-Feb-16","15-Jan-15");
	}
	public function gen_pdf($cust_id,$bill_yr,$bill_mon,$amount_due,$bill_bal,$due_date,$bill_date,$read_date,$prev_date){
		ob_start(); 
		$acct = $this->wus->get_account($cust_id);
		foreach($acct as $r){
			$area = $r->zone_1;
			$account_num = $r->acct_num;
			$account_name = $r->acct_name;
		}
		$foryear = explode('20',$bill_yr);
		$inv = $this->wus->get_inv($foryear[1].$bill_mon.$account_num);
		$month = $bill_mon  - 1;
        $mons = $month;
        $year = $bill_yr;
        if($mons == 0){
        $year = $bill_yr - 1;
        $month = 12;
        }
		$tmp = explode('20',$year);
	    //var_dump($tmp[1].($month < 10 ? "0".$month : $month).$_GET['acct_num']);
		$bal = $this->wus->get_bal($tmp[1].($month < 10 ? "0".$month : $month).$account_num,$prev_date,$read_date);
		//var_dump($result);
		$table_data = '';
		$table_data2 = '';
		$table_data3 = '';
		$table_data4 = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		$remain_bal = 0;
		$tots = 0;
		//var_dump($bal);
		foreach ($bal as $i) {
			//$remain_bal = $bill_bal - $i->pay_amount;
			$table_data2 = '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Amount Due From Previous Bill</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->curchg, 2, ".", "," ).'</td></tr>';
		}
		foreach ($bal as $i) {
			//$remain_bal = $bill_bal - $i->pay_amount;
			$table_data3 .= '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Payment ('.$i->receipt_date.')</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->pay_amount, 2, ".", "," ).'</td></tr>';
			$tots += $i->pay_amount;
		}
		foreach ($bal as $i) {
			$remain_bal = $i->curchg - $tots;
			$table_data4 = '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Remaining Balance From Previous Bill<br><i style="text-align: center; padding-top: 5px; font-size: 12px;">Payment as of </i></td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($bill_bal, 2, ".", "," ).'</td>
			</tr>';
		}

		$total_cur_chg = 0;
		foreach ($inv as $i) {
			switch ($i->charge_code) {
				case 'WATCONS':
					$code = "Water Consumption";
					break;
				case 'SURCHG':
					$code = "Surcharge";
					break;
				case 'LPAYCHG':
					$code = "Late Payment Charge";
					break;
			}
			$table_data .= '<tr>
			<td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">'.$code.'</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->line_amt, 2, ".", "," ).'</td></tr>';
			$total_cur_chg += $i->line_amt;
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=100%; padding-top:-5px;'>
						<h4>BRGY LUMBIA WATER SYSTEM</h4>
						<h4>Lumbia, Cagayan de Oro City</h4>
					</td>
				</tr>
			</table><br><br>
			<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=100%; padding-top:-5px;'>
						<h4>Bill Statement</h4>
						from: <b>".$prev_date."</b> to: <b>".$read_date."</b>
					</td>
				</tr>
			</table><br><br>
			<table style='width: 100%;'>
				<tr>
			        <td style='text-align: right;width=30%;'>Bill ID</td>
			        <td style='text-align: left;width=70%;'><b>".$foryear[1].$bill_mon.$account_num."</b></td>
				</tr>
	            <tr>
			        <td style='text-align: right;width=30%;'>Account Number:</td>
			        <td style='text-align: left;width=70%;'><b>".$account_num."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Account Name:</td>
			        <td style='text-align: left;width=70%;'><b>".$account_name."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Area:</td>
			        <td style='text-align: left;width=70%;'><b>".$area."</b></td>
				</tr>
			</table><br><br>
			<table style='width: 100%;'>
				<tr>
			        <td style='text-align: right;width=30%;'>Bill Date</td>
			        <td style='text-align: left;width=70%;'><b>".$bill_date."</b></td>
				</tr>
	            <tr>
			        <td style='text-align: right;width=30%;'>Due Date:</td>
			        <td style='text-align: left;width=70%;'><b>".$due_date."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Amount Due:</td>
			        <td style='text-align: left;width=70%;'><b>".number_format($total_cur_chg + $bill_bal, 2, ".", "," )."</b></td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=60%; padding: 5px;'><b>Previous Bill Charge</b></td>
					<td style='border-bottom:1px solid; text-align: right;width=40%; padding: 5px;'><b>Amount</b></td>
				</tr>" .
			$table_data2.$table_data3.$table_data4
			. "</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=60%; padding: 5px;'><b>Current Bill Charge</b></td>
					<td style='border-bottom:1px solid; text-align: right;width=40%; padding: 5px;'><b>Amount</b></td>
				</tr>" .
			$table_data
			. "<tr>
					<td style='border-top:1px solid; text-align: center;width=60%; padding: 5px;'><b>Total Current Bill Charge</b></td>
					<td style='border-top:1px solid; text-align: right;width=40%; padding: 5px;'><b>".number_format($total_cur_chg, 2, ".", "," )."</b></td>
				</tr></table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-top:1px solid; text-align: center;width=60%; padding: 5px;'><b>Total Amount Due</b></td>
					<td style='border-top:1px solid; text-align: right;width=40%; padding: 5px;'><b>".number_format($total_cur_chg + $bill_bal, 2, ".", "," )."</b></td>
				</tr></table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Bills');
		$html2pdf->WriteHTML($content);
		$pdf = $html2pdf->Output('','S');
		//$pdf = $html2pdf->Output('WATER_BILL_' . date("dmY_His") ."_".$foryear[1].$bill_mon.$account_num. '.pdf', 'S');
		$data = array(
			':p_file' => $pdf,
			':p_file_name' => 'WATER_BILL',
			':p_file_type'	=> 'pdf',
			':p_account_num' => $account_num,
			':p_account_id'	=> $cust_id,
			':p_bill_id'=> $foryear[1].$bill_mon.$account_num
		);
		$this->wus->save_pdf($data);
	}

	public function error_404(){

		$this->load->view('404');

	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */